package com.zhengtoudai.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.MvpActivity;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.ui.bean.AccountSettingModel;
import com.zhengtoudai.ui.bean.Result;
import com.zhengtoudai.ui.presenter.AccountSettingPresenter;
import com.zhengtoudai.ui.presenter.LoginOutPresenterImpl;
import com.zhengtoudai.ui.view.AccountSettingView;
import com.zhengtoudai.ui.view.CommonView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.util.Tool;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AccountSettingActivity extends MvpActivity<AccountSettingPresenter> implements AccountSettingView, CommonView{

    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(R.id.tv_phone)
    TextView mTvPhone;
    @Bind(R.id.tv_email)
    TextView mTvEmail;
    @Bind(R.id.tv_real_name)
    TextView mTvRealName;
    @Bind(R.id.tv_login_password)
    TextView mTvLoginPassword;
    @Bind(R.id.rl_real_name)
    RelativeLayout mRlRealName;

    private Activity mActivity;

    @Inject
    LoginOutPresenterImpl mOutPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_setting);
        mActivity=AccountSettingActivity.this;
        ButterKnife.bind(this);
    }

    @Override
    protected AccountSettingPresenter createPresenter() {
        return new AccountSettingPresenter(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("账户设置");
        DaggerActivityComponent.builder().appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(this))
                .build()
                .inject(this);
        mOutPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mOutPresenter.detachView();
    }

    @Override
    protected void initView() {

//        mTvRealName.setEnabled(false);
        mvpPresenter.loadData(mActivity);

    }

    @OnClick({R.id.rl_email, R.id.rl_login_password, R.id.rl_real_name, R.id.btn_safety_login_out})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        switch (view.getId()) {
            case R.id.rl_email:

                break;
            case R.id.rl_real_name:

                startActivity(RealNameCertificationActivity.class);

                break;
            /*case R.id.rl_bank_card:
                startActivity(BindBankCardActivity.class);
                break;*/
            case R.id.rl_login_password:
                startActivity(ChangeLoginPassWordActivity.class);
                break;
//            case R.id.rl_pay_password:
//                startActivity(ChangePayPasswordActivity.class);
//                break;
            case R.id.btn_safety_login_out:
                showAlertDialog("是否退出登录", "取消", "退出", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mOutPresenter.loginOut();
                    }
                });
                break;
        }
    }

    @Override
    public void getDataSuccess(AccountSettingModel model) {
        AccountSettingModel.AccountSettingBean mInfo = model.getResult();
        Log.e("TAG", "返回数据=" + mInfo.getCenterMessage().toString());
        mTvName.setText(mInfo.getCenterMessage().getUserAccount());
        mTvPhone.setText(mInfo.getCenterMessage().getUserPhone());
        //实名认证状态
        String realNameStatus = mInfo.getCenterMessage().getRealnameStatus();
        String emailStatus=mInfo.getCenterMessage().getEmailStatus();
        if (realNameStatus.equals("2")) {
            mTvRealName.setText("已绑定");
            mRlRealName.setEnabled(false);
        }
        if (emailStatus.equals("-2")){
            mTvEmail.setText("未设置");
        }else{
            mTvEmail.setText(mInfo.getCenterMessage().getUserEmail());
        }

    }

    @Override
    public void getDataFail(int code) {
        Log.e("TAG", "得到请求返回状态码:" + code);
        if (code == 401) {
            startActivity(LoginActivity.class);
            finish();
        }

    }

    @Override
    public void showLoading() {
        showProgressDialog("正在加载中...");
    }

    @Override

    public void hideLoading() {
        dismissProgressDialog();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (App.mApp.hasLogin()){
            mvpPresenter.loadData(mActivity);
        }else{
            finish();
        }
    }


    @Override
    public void showDialog() {
        showProgressDialog("正在退出中...");
    }

    @Override
    public void onResult(Result result) {
        if (result.getCode() == Config.SUCCESSFUL_CODE) {
            startActivity(LoginActivity.class);
            App.mApp.logout();
            finish();
        }
        AbToastUtil.showToast(context, result.getMessage());
    }

    @Override
    public void onFailure(Throwable e) {
        HttpUtil.disposeOnFailure(context, e);
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }
}
